package client.test;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JTextArea;

import clientInterfaces.InterfaceAffichageClient;

public class ImplAffichageClient extends UnicastRemoteObject implements InterfaceAffichageClient {

	private String name;
	private JTextArea textArea;
	private JTextArea pseudoArea;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1649553000187415608L;
	
	
	public ImplAffichageClient(JTextArea textArea, String pseudo, JTextArea pseudoArea) throws RemoteException {
		super();
		this.name=pseudo;
		this.textArea=textArea;
		this.pseudoArea=pseudoArea;
	}

//	public ImplAffichageClient(String name,JTextArea textArea) throws RemoteException {
//		super();
//		this.name=name;
//		this.textArea=textArea;
//	}

	@Override
	public void afficheMessage(String message) throws RemoteException {
		System.out.println(message);
		if(textArea.getText().equals("")) {
			textArea.setText(message);
		} else {
			textArea.setText(textArea.getText()+"\n"+message);
		}
	}



	@Override
	public String getName() throws RemoteException {
		return name;
	}


	@Override
	public void updateListeClient(LinkedList<InterfaceAffichageClient> listeClients) throws RemoteException {
		Iterator<InterfaceAffichageClient> itr = listeClients.iterator();
		String listePseudo = "";
		while(itr.hasNext()) {
			InterfaceAffichageClient iac = itr.next();
			listePseudo += iac.getName()+"\n";
		}
		pseudoArea.setText(listePseudo);
	}
	
}
